package com.vcms.view.simulation.customer;

import java.awt.Button;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.Panel;
import java.awt.TextField;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import com.vcms.controller.customer.TransactionController;
import com.vcms.controller.storemanagement.StoreController;
import com.vcms.entity.store.item.StoreItem;
import com.vcms.entity.store.itemtype.DrinksBrand;
import com.vcms.utility.formatter.MoneyFormatter;
import com.vcms.view.simulation.uicomponent.listener.DrinkSelectionListener;

@SuppressWarnings("serial")
public class SimpleDrinkSelectionBox extends DrinkSelectionBox {

	public SimpleDrinkSelectionBox(Container composite, TransactionController tCtrl) {
		super(composite, tCtrl);
		this.initDrinkSelection();
		this.setLayout(new GridLayout(0, 1));
	}

	public void initDrinkSelection() {
		super.initDrinkSelection();
	}
	
	public void initUI() {
		StoreItem[] items = transactionController.getMainControler()
				.getStoreController().getStoreItems(StoreController.DRINK);

		drinkId = new int[items.length];

		Panel tp = new Panel(new GridLayout(drinkId.length, 1));
		drinkSelection = new SimpleDrinkSelection[drinkId.length + 1];

		for (int i = 0; i < items.length; i++) {
			DrinksBrand ob = (DrinksBrand) items[i].getContent();
			drinkSelection[i] = new SimpleDrinkSelection();
			update( i, items[i].getQuantity(), ob.getPrice(), ob.getName() );
			drinkId[i] = i;
			((SimpleDrinkSelection)drinkSelection[i]).addListener(new DrinkSelectionListener(this));
			tp.add(drinkSelection[i]);
		}
		this.add(tp);
	}
	
	
	protected void setAction() {
		int i;
		for (i = 0; i < drinkId.length; i++) {
			((SimpleDrinkSelection)drinkSelection[i]).setActionCommand(String.valueOf(i));
		}
	}
	
	public void update(int brandIndex, int quantity, int price, String name){
		super.update(brandIndex, quantity, price, name);
	}
	
	public void setActive(boolean active) {
		super.setActive(active);
	}
	
	public void doActionPerformed(ActionEvent e) {
		transactionController.startTransaction(  Integer.parseInt(  ((Button) e.getSource()).getActionCommand() )  );
	}
	

	/* private class for drink selection */
	private static class SimpleDrinkSelection extends DrinkSelection {

		public final static Color ACT_COLOR = Color.white;

		@SuppressWarnings("unused")
		public final static Color IN_STK = Color.GRAY;

		@SuppressWarnings("unused")
		public final static Color OUT_STK = Color.RED;

		private Button drinkName;
		private TextField drinkPrice;
		private TextField drinkStatus;

		public SimpleDrinkSelection() {
			drinkName = new Button();
			drinkName.setFont(new Font("Helvetica", Font.BOLD, 14));
			drinkName.setBackground(ACT_COLOR);

			drinkPrice = new TextField("     ");
			drinkPrice.setEditable(false);

			drinkStatus = new TextField("Out Of Stock");
			drinkStatus.setEditable(false);

			this.setLayout(new GridLayout(1, 3));
			Panel tp = new Panel(new GridLayout(0, 1));
			tp.add(drinkName);
			Panel tp1 = new Panel();
			tp1.add(drinkPrice);
			Panel tp2 = new Panel();
			tp2.add(drinkStatus);
			this.add(tp);
			this.add(tp1);
			this.add(tp2);
		}

		public void setItemState(boolean active) {
			super.setItemState(active);
			drinkName.setEnabled(active);
			drinkStatus.setText(active ? "Available" : "Out Of Stock");
			drinkStatus.setEnabled(!active);
		}
		
		public boolean getItemState() {
			return super.getItemState();
		}

		public void setName(String name) {
			super.setName(name);
			drinkName.setLabel(name);
		}

		public void setPrice(int price) {
			super.setPrice(price);
			drinkPrice.setText(MoneyFormatter.toMoney(price));
		}
		
		public void setQuantity(int quantity)
		{
			super.setQuantity(quantity);
		}
		
		public void setItemActive(boolean active) {
			drinkName.setEnabled(active);
			drinkStatus.setEnabled(active);
		}

		public void addListener(ActionListener l) {
			drinkName.addActionListener(l);
		}

		public void setActionCommand(String cmd) {
			drinkName.setActionCommand(cmd);
		}

	}

}
